package cn.wolfcode.edu.web.controller;

import cn.wolfcode.edu.domain.Calendar;
import cn.wolfcode.edu.service.ICalendarService;
import cn.wolfcode.edu.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("calendar")
public class CalendarController {
    @Autowired
    private ICalendarService calendarService;
    @RequestMapping("")
    public String calendar() {
        return "calendar";
    }
    @RequestMapping("list")
    @ResponseBody
    public List listAll() {
        return calendarService.listAll();
    }
    //日程修改
    @RequestMapping("change")
    @ResponseBody
    public JsonResult change(Date start, Date end, Long id) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String x = simpleDateFormat.format(start);
        String y = simpleDateFormat.format(end);
        Calendar calendar = new Calendar();
        start = simpleDateFormat.parse(x);
        end = simpleDateFormat.parse(y);
        calendar.setEnd(end);
        calendar.setStart(start);
        calendar.setId(id);
        JsonResult jsonResult = new JsonResult();
        try {
            calendarService.change(calendar);
        } catch (Exception e) {
            jsonResult.setMsg("日程安排修改失败，请重新再试！");
        }
        return jsonResult;
    }
    //日程保存
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Calendar calendar) throws ParseException {
        /*SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String x = simpleDateFormat.format(start);
        String y = simpleDateFormat.format(end);
        Calendar calendar = new Calendar();
        start = simpleDateFormat.parse(x);
        end = simpleDateFormat.parse(y);
        calendar.setEnd(end);
        calendar.setStart(start);
        calendar.setTitle(title);*/
        JsonResult jsonResult = new JsonResult();
        try {
            calendarService.save(calendar);
        } catch (Exception e) {
            jsonResult.setMsg("日程安排修改失败，请重新再试！");
        }
        return jsonResult;
    }
}
